import Carousel from '@/components/carousel'
import Comment from '@/components/comment'
import Digg from '@/components/digg'
import Hits from '@/components/hits'
import Lazyload from '@/components/lazyload'
import Love from '@/components/love'
import Page from '@/components/page'
import Play from '@/components/play'
import Playlog from '@/components/playlog'
import Popup from '@/components/popup'
import Rating from '@/components/rating'
import Search from '@/components/search'
import Share from '@/components/share'
import User from '@/components/user'
import Week from '@/components/week'

import rankTop from '@/components/rankTop'
import lrScroll from '@/components/lrScroll'
import { display, scrollPos } from '@/utils'
import '@/static/css/main'
/**
 * 全局方法开始
 * @function Hits 加载访问次数
 * @function Digg 播放页点赞，明星送花，新闻顶踩
 * @function Share 分享
 * @function Lazyload 懒加载
 * @function Love 订阅收藏
 * @function Playlog 观看记录
 * @function User 登录注册
 * @function Popup 信息弹出层
 * @function Search 搜索联想
 * @function 其他方法
 */
new Hits()
new Digg()
new Share()
new Love()
new Playlog()
const loayload = new Lazyload()
loayload.throttle(loayload.lazyload(), 500, 500)
const user = new User()
$('body').on('click', '#loginbarx', function () {
  user.loginform()
})
$('body').on('click', '.no-user', function () {
  user.rgeform()
})
$('body').on('click', '.yes-user', function () {
  user.loginform()
})
$('body').on('click', 'img.validate-img, img#verify', function () {
  user.refresh($(this))
})
if ($('.d-item-list a').get(0) || $('.d-index-item a').get(0)) {
  new Popup($('.d-index-item a, .d-item-list a'), 500)
}
if ($('.search-text1').get(0)) {
  new Search()
}

const play = new Play()
$('.d-playlist li').click(function () {
  const id = $(this).data('id')
  const type = $(this).data('type')
  const pid = $(this).data('pid')
  const y = $(this).offset().top
  const x = $(this).offset().left
  play.play({ id, type, pid, elem: $('.d-play-pop'), x, y })
})

// 排行榜
rankTop($('.d-hotlist li'))
// 左右不等高的时候滚动
lrScroll('.d-warp-left', '.d-warp-right', 25, -25)
lrScroll('.d-news-left', '.index-right', 10, -10)
lrScroll('.d-left', '.d-right', 10, -10)
lrScroll('.filter-nav', '.filter-box', 20, -20)
// 显示隐藏
display($('.qr-code-ico'), $('.qr-code'), 'qr-code-ico-hover')
display($('.shang'), $('#shang'))
display($('.weixin'), $('#weixin'))
// 点击跳到锚点处
scrollPos($("[href*='#']"))
// 回到顶部
$(window).scroll(function () {
  if ($(window).scrollTop() > 100) {
    $('#back-to-top').fadeIn(100)
  } else {
    $('#back-to-top').fadeOut(100)
  }
})
$('#back-to-top').click(function () {
  $('body,html').animate({ scrollTop: 0 }, 100)
  return false
})

let timer = null
$('.drop-down').hover(
  function () {
    clearTimeout(timer)
    $('.drop-box').hide()
    $(this).find('.drop-title').addClass('drop-title-hover')
    $(this).find('.drop-box').show()
  },
  function () {
    $(this).find('.drop-title').removeClass('drop-title-hover')
    timer = setTimeout(
      $.proxy(function () {
        $(this).find('.drop-box').hide()
      }, this),
      300
    )
  }
)
$(document).click(function () {
  $('.drop-box').hide()
})
$('.drop-box').click(function (event) {
  event.stopPropagation()
  $(this).show()
})
/**
 * 全局结束
 */

/**
 * 首页开始
 * @function Carousel 轮播滚动
 * @function Week 星期追番表
 */

if ($('.newlist').get(0)) {
  Carousel('.newlist', false, 4000)
}
if ($('#weektab li').get(0)) {
  new Week($('#weektab li'))
}
/**
 * 首页结束
 */

/**
 * 详情页开始
 * @fucntion Rating 加载评分
 * @fucntion Comment 加载评论
 * @fucntion PageMore 更多剧集方法
 * 播放源切换
 */
if ($('ul.rating li').get(0)) {
  new Rating()
}
if ($('#comment').get(0)) {
  new Comment()
}
if ($('.qr-code-box').get(0)) {
  $('.qr-code-box').qrcode({
    width: 140,
    height: 140,
    text: $('.qr-code-box').data('url'),
  })
}
/**
 * 详情页结束
 */

/**
 * 剧情开始
 * @fucntion Page 简单分页
 */
if ($('#ep-page').get(0)) {
  Page('#ep-page', '#tab_conbox')
}
/**
 * 剧情结束
 */
